#include "pagehvac.h"
#include "ui_pagehvac.h"

PageHvac::PageHvac(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageHvac)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_lHVACStatNameList<<"室内温度(℃)"
                       <<"目标温度(℃)"
                       <<"空调机组状态"
                       <<"控制模式"
                       <<"减载状态"
                       <<"机组新风阀状态"
                       <<"机组回风阀状态"
                       <<"压缩机状态"
                       <<"冷凝风机状态"
                       <<"通风机状态"
                       <<"PM2.5(μg/m³)"
                       <<"CO2(PPM)"
                       <<"TVOC(ppb)"
                       <<"压差(ppb)"
                       <<"湿度(%)"
                       <<"当月机组1能耗(kwh)"
                       <<"当月机组2能耗(kwh)";

    m_lHVACStatCntOfOneCar<<1
                          <<1
                          <<2
                          <<1
                          <<1
                          <<4
                          <<2
                          <<4
                          <<4
                          <<4
                          <<1
                          <<1
                          <<1<<1<<1<<1<<1;

    InitHVACStatLabels();
    InitBtnsMapper();
    ui->SalWidget->hide();
    setPageActive(false);

    m_nHVACMode = HVAC_Stop;
    m_nWhichSalSelected = WholeTrain;

    setDefaultStat();
}

PageHvac::~PageHvac()
{
    delete ui;
}

void PageHvac::setDefaultStat()
{
    for(int i=0;i<HVACStat_Count;i++)
    {
        for(int j=0;j<m_lHVACStatList[i].length();j++)
            setHVACStat(i,j,0);
    }
}

void PageHvac::InitHVACStatLabels()
{
    m_nStatLabelWidth_Car = MiniCarSaloonWidth+MiniCarHSpace_1220;
    m_nStatLabelSpaceX = MiniCarLeftSpace_1220 - MiniCarHSpace_1220/2;
    m_nStatLabelHeight = 35;
    m_nStatLabelStartY = 10;
    m_nStatLabelSpaceY = 0;

    uint8_t statCnt = m_lHVACStatNameList.length();
    int totalpage = statCnt%ROWS_ONE_PAGE?(statCnt/ROWS_ONE_PAGE+1):statCnt/ROWS_ONE_PAGE;
    ui->Control_widget->setVisible(totalpage>1);
    ui->totalPage->setText(QString().setNum(totalpage));
    for(int i=0;i<statCnt;i++)
    {
        if(i%ROWS_ONE_PAGE == 0)
        {
            QWidget *w = new QWidget(this);
            w->setGeometry(0,0,this->width()-140,600);
            w->setVisible(i==0);
            m_lDisplayWidgets.append(w);
        }
        QLabel *statName = new QLabel(m_lDisplayWidgets[i/ROWS_ONE_PAGE]);
        statName->setStyleSheet(LabelNBStyle);
        statName->setAlignment(Qt::AlignCenter);
        statName->setText(m_lHVACStatNameList.at(i));
        statName->setGeometry(m_nStatLabelSpaceX-180,
                              m_nStatLabelStartY+(i%ROWS_ONE_PAGE)*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                              180,m_nStatLabelHeight);

        uint8_t statOneCarCnt = m_lHVACStatCntOfOneCar.at(i);
        QList<QLabel*> OneCarLabel;
        OneCarLabel.clear();
        for(int j=0;j<SaloonCnt;j++)
        {
            for(int k=0;k<statOneCarCnt;k++)
            {
                QLabel *stat = new QLabel(m_lDisplayWidgets[i/ROWS_ONE_PAGE]);
                stat->setStyleSheet(StatLabelStyle);
                stat->setAlignment(Qt::AlignCenter);
                SetPosition(stat,CAR(j),ROW(i%ROWS_ONE_PAGE),COL(k),TOTAL(statOneCarCnt),j>=(SaloonCnt/2));
                OneCarLabel.append(stat);
            }
        }
        m_lHVACStatList.append(OneCarLabel);
    }
}

//row 和 col 都从0开始
void PageHvac::SetPosition(QWidget* w,int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    float OnestatLabelWidth = m_nStatLabelWidth_Car/total;
    float rowSpaceX = (m_nStatLabelWidth_Car - total*OnestatLabelWidth)/(total+1);
    if(!xReverse)
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+col*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
    else
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+(total-1-col)*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
};

void PageHvac::InitBtnsMapper()
{
    m_lSalSelBtnList.append(ui->Btn_T);
    m_lSalSelBtnList.append(ui->Btn_TC1);
    m_lSalSelBtnList.append(ui->Btn_Mp1);
    m_lSalSelBtnList.append(ui->Btn_M1);
    m_lSalSelBtnList.append(ui->Btn_M2);
    m_lSalSelBtnList.append(ui->Btn_Mp2);
    m_lSalSelBtnList.append(ui->Btn_TC2);

    m_nWhichSalSelected = 0;
    m_pSalSelMapper = new QSignalMapper(this);
    for (int i = 0; i < m_lSalSelBtnList.size(); ++i) {
        connect(m_lSalSelBtnList[i], SIGNAL(clicked()), m_pSalSelMapper, SLOT(map()));
        m_pSalSelMapper->setMapping(m_lSalSelBtnList[i], i);
    }
    connect(m_pSalSelMapper, SIGNAL(mapped(int)),this, SLOT(slot_SalSelect(int)));


    m_lModeBtnList.append(ui->Btn_Mode_Auto);
    m_lModeBtnList.append(ui->Btn_Mode_Man);
    m_lModeBtnList.append(ui->Btn_Mode_Vent);
    m_lModeBtnList.append(ui->Btn_Mode_Stop);
    m_lModeBtnList.append(ui->Btn_OnlyTC);

    m_pSalModeMapper = new QSignalMapper(this);
    for (int i = 0; i < m_lModeBtnList.size(); ++i) {
        connect(m_lModeBtnList[i], SIGNAL(clicked()), m_pSalModeMapper, SLOT(map()));
        m_pSalModeMapper->setMapping(m_lModeBtnList[i], i+1);
    }
    connect(m_pSalModeMapper, SIGNAL(mapped(int)),this, SLOT(slot_SalMode(int)));


    m_lHVACTempList.append(ui->SalTemp_T);
    m_lHVACTempList.append(ui->SalTemp_1);
    m_lHVACTempList.append(ui->SalTemp_2);
    m_lHVACTempList.append(ui->SalTemp_3);
    m_lHVACTempList.append(ui->SalTemp_4);
    m_lHVACTempList.append(ui->SalTemp_5);
    m_lHVACTempList.append(ui->SalTemp_6);
}

void PageHvac::on_BtnNext_clicked()
{
    int curpage = ui->curPage->text().toInt();
    int totalpage = ui->totalPage->text().toInt();
    if(curpage < totalpage)
        curpage++;
    for(int i=0;i<m_lDisplayWidgets.length();i++)
        m_lDisplayWidgets[i]->setVisible(i==curpage-1);
    ui->curPage->setText(QString().setNum(curpage));
}

void PageHvac::on_BtnPre_clicked()
{  
    int curpage = ui->curPage->text().toInt();
    if(curpage > 1)
        curpage--;
    for(int i=0;i<m_lDisplayWidgets.length();i++)
        m_lDisplayWidgets[i]->setVisible(i==curpage-1);
    ui->curPage->setText(QString().setNum(curpage));
}

void PageHvac::setPageActive(bool xActive)
{
    for(int i=0;i<m_lSalSelBtnList.length();i++)
        m_lSalSelBtnList[i]->setEnabled(xActive);

    for(int i=0;i<m_lModeBtnList.length();i++)
        m_lModeBtnList[i]->setEnabled(xActive);

    ui->Btn_OpenDAMP->setEnabled(xActive);
    ui->Btn_CloseDAMP->setEnabled(xActive);
    ui->Btn_Mode_Stop->setEnabled(xActive);
    ui->Btn_Mode_Vent->setEnabled(xActive);
    ui->Btn_Mode_Man->setEnabled(xActive);
    ui->Btn_Mode_Auto->setEnabled(xActive);
    ui->Btn_OnlyTC->setEnabled(xActive);
    ui->Btn_Add->setEnabled(xActive);
    ui->Btn_Sub->setEnabled(xActive);
    ui->Btn_T->setEnabled(xActive);
    ui->Btn_TC1->setEnabled(xActive);
    ui->Btn_Mp1->setEnabled(xActive);
    ui->Btn_M1->setEnabled(xActive);
    ui->Btn_M2->setEnabled(xActive);
    ui->Btn_Mp2->setEnabled(xActive);
    ui->Btn_TC2->setEnabled(xActive);
}

void PageHvac::slot_SalSelect(int sal)
{
    m_nWhichSalSelected = sal;
}

void PageHvac::slot_SalMode(int mode)
{
    ui->SalWidget->setVisible(mode == HVAC_Man);
    m_nHVACMode = mode;
    emit signal_SendHvacMode(mode,true);
    ui->Btn_Mode_Stop->setEnabled(false);
    ui->Btn_Mode_Vent->setEnabled(false);
    ui->Btn_Mode_Man->setEnabled(false);
    ui->Btn_Mode_Auto->setEnabled(false);
    ui->Btn_OnlyTC->setEnabled(false);
    GlobalConfig::addOperationInfo(QString("设置空调模式:%1").arg(mode));
    QTimer::singleShot(2000,this,[=](){
        ui->Btn_Mode_Stop->setEnabled(true);
        ui->Btn_Mode_Vent->setEnabled(true);
        ui->Btn_Mode_Man->setEnabled(true);
        ui->Btn_Mode_Auto->setEnabled(true);
        ui->Btn_OnlyTC->setEnabled(true);
        emit signal_SendHvacMode(mode,false);
    });
}

void PageHvac::on_Btn_Sub_clicked()
{
    if(m_nHVACMode == HVAC_Man)
        SubTemp();
}

void PageHvac::on_Btn_Add_clicked()
{
    if(m_nHVACMode == HVAC_Man)
        AddTemp();
}

void PageHvac::AddTemp()
{
    int temp = m_lHVACTempList[m_nWhichSalSelected]->text().toFloat()*2;
    if(temp < 56.0)
        temp++;
    m_lHVACTempList[m_nWhichSalSelected]->setText(QString().setNum(temp*0.5));
    GlobalConfig::addOperationInfo(QString("设置%1车空调温度:%2").arg(m_nWhichSalSelected).arg(temp));
    if(m_nWhichSalSelected == 0)
    {
        for(int i=0;i<m_lHVACTempList.length();i++)
        {
          m_lHVACTempList[i]->setText(QString().setNum(temp*0.5));
          emit signal_HvacTemp(i,temp);
          GlobalConfig::setSalHVACTemp(i,temp);
        }
        return;
    }
    GlobalConfig::setSalHVACTemp(m_nWhichSalSelected,temp);
    emit signal_HvacTemp(m_nWhichSalSelected,temp);
}


void PageHvac::SubTemp()
{
    int temp = m_lHVACTempList[m_nWhichSalSelected]->text().toFloat()*2;
    if(temp > 44)
        temp--;
    m_lHVACTempList[m_nWhichSalSelected]->setText(QString().setNum(temp*0.5));
    GlobalConfig::addOperationInfo(QString("设置%1车空调温度:%2").arg(m_nWhichSalSelected).arg(temp));
    if(m_nWhichSalSelected == 0)
    {
        for(int i=0;i<m_lHVACTempList.length();i++)
        {
          m_lHVACTempList[i]->setText(QString().setNum(temp*0.5));
          emit signal_HvacTemp(i,temp);
          GlobalConfig::setSalHVACTemp(i,temp);
        }
        return;
    }
    GlobalConfig::setSalHVACTemp(m_nWhichSalSelected,temp);
    emit signal_HvacTemp(m_nWhichSalSelected,temp);
}

void PageHvac::syncHVACMode(int mode)
{
    if(GlobalConfig::cabActivated == GlobalConfig::cabNumber && !GlobalConfig::xFAM)
        return;
    if(mode < 1 || mode > 5)
        return;
    m_lModeBtnList[mode-1]->setChecked(true);
    ui->SalWidget->setVisible(mode == HVAC_Man);
    m_nHVACMode = mode;
    GlobalConfig::addOperationInfo(QString("同步空调模式:%1").arg(mode));
}

void PageHvac::syncHVACTemp(int sal, int temp)
{
    if(GlobalConfig::cabActivated == GlobalConfig::cabNumber && !GlobalConfig::xFAM)
        return;
    if(temp < 44 || temp > 56)
        return;
    m_lHVACTempList[sal]->setText(QString().setNum(temp*0.5));
    GlobalConfig::setSalHVACTemp(sal,temp);
    emit signal_HvacTemp(sal,temp);
}

void PageHvac::setHVACStat(int item, int index, uint16_t value)
{
    if(index >= m_lHVACStatList[item].length())
        return;
    if(item == HVACStat_SalTemp)
    {
        if(value == 0xFF)
            m_lHVACStatList[item][index]->setText("?");
        else
            m_lHVACStatList[item][index]->setText(QString("%1").arg(value-30));
    }
    if(item == HVACStat_TargetTemp)
    {
        if(value == 0xFF)
            m_lHVACStatList[item][index]->setText("?");
        else
            m_lHVACStatList[item][index]->setText(QString("%1").arg(value*0.5));
    }
    else if(item == HVACStat_UnitStat)
    {
        m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:black;border:1px solid rgb(147,147,147);font:12pt 'Arial';color:white;}");
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("紧急通风");
            break;
        case 3:
            m_lHVACStatList[item][index]->setText("预冷");
            break;
        case 4:
            m_lHVACStatList[item][index]->setText("制冷");
            break;
        case 5:
            m_lHVACStatList[item][index]->setText("通风");
           break;
        case 6:
            m_lHVACStatList[item][index]->setText("停止");
            break;
        case 7:
            m_lHVACStatList[item][index]->setText("预暖");
            break;
        case 8:
            m_lHVACStatList[item][index]->setText("制暖");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_ControlMode)
    {
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 1:
            m_lHVACStatList[item][index]->setText("集控位");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("本控位");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_LowerLoad)
    {
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 1:
            m_lHVACStatList[item][index]->setText("激活");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("未激活");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_NADamper)
    {
        m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:black;border:1px solid rgb(147,147,147);font:13pt 'Arial';color:white;}");
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 1:
            m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:red;border:1px solid rgb(147,147,147);font:13pt 'Arial';color:black;}");
            m_lHVACStatList[item][index]->setText("故障");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("关闭");
            break;
        case 3:
            m_lHVACStatList[item][index]->setText("全开");
            break;
        case 4:
            m_lHVACStatList[item][index]->setText("2/3");
            break;
        case 5:
            m_lHVACStatList[item][index]->setText("1/3");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_RADamper)
    {
        m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:black;border:1px solid rgb(147,147,147);font:13pt 'Arial';color:white;}");
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 1:
            m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:red;border:1px solid rgb(147,147,147);font:13pt 'Arial';color:black;}");
            m_lHVACStatList[item][index]->setText("故障");
            break;
        case 3:
            m_lHVACStatList[item][index]->setText("打开");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("关闭");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_Compressor || item == HVACStat_Condenserfan)
    {
        m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:black;border:1px solid rgb(147,147,147);font:12pt 'Arial';color:white;}");
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 3:
            m_lHVACStatList[item][index]->setText("打开");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("关闭");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_Ventor)
    {
        m_lHVACStatList[item][index]->setStyleSheet("QLabel{background-color:black;border:1px solid rgb(147,147,147);font:12pt 'Arial';color:white;}");
        switch (value) {
        case 0:
            m_lHVACStatList[item][index]->setText("?");
            break;
        case 4:
            m_lHVACStatList[item][index]->setText("高速");
            break;
        case 3:
            m_lHVACStatList[item][index]->setText("低速");
            break;
        case 2:
            m_lHVACStatList[item][index]->setText("关闭");
            break;
        default:
            m_lHVACStatList[item][index]->setText("?");
            break;
        }
    }
    else if(item == HVACStat_PM25)
    {
        if(value == 0xFFFF)
        {
            m_lHVACStatList[item][index]->setText("?");
            return;
        }

        if(value > 1000) return;
         m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == HVACStat_CO2)
    {
        if(value == 0xFFFF)
        {
            m_lHVACStatList[item][index]->setText("?");
            return;
        }
        if(value > 5000) return;
         m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == HVACStat_TVOC)
    {
        if(value == 0xFFFF)
        {
            m_lHVACStatList[item][index]->setText("?");
            return;
        }
        if(value > 2000) return;
         m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == HVACStat_PressureDifference)
    {
        if(value == 0xFFFF)
        {
            m_lHVACStatList[item][index]->setText("?");
            return;
        }
        if(value > 2000) return;
         m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == HVACStat_Humidity)
    {
        if(value == 0xFF)
        {
            m_lHVACStatList[item][index]->setText("?");
            return;
        }
        if(value > 100) return;
         m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
    if(item == HVACStat_EnergyMonth1 || item == HVACStat_EnergyMonth2)
    {
        if(value == 0xFFFF)
            m_lHVACStatList[item][index]->setText("?");
        else
            m_lHVACStatList[item][index]->setText(QString("%1").arg(value));
    }
}

void PageHvac::on_Btn_CloseDAMP_clicked()
{
    emit signal_CloseNADamper(true);
    ui->Btn_CloseDAMP->setEnabled(false);
    ui->Btn_OpenDAMP->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_CloseNADamper(false);
        ui->Btn_CloseDAMP->setEnabled(true);
        ui->Btn_OpenDAMP->setEnabled(true);
    });
}

void PageHvac::on_Btn_OpenDAMP_clicked()
{
    emit signal_OpenNADamper(true);
    ui->Btn_CloseDAMP->setEnabled(false);
    ui->Btn_OpenDAMP->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_OpenNADamper(false);
        ui->Btn_CloseDAMP->setEnabled(true);
        ui->Btn_OpenDAMP->setEnabled(true);
    });
}
